<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>两个非重叠子数组的最大和</title>
</head>
<body>
  <script>
    function fn(nums, firstLen, secondLen) {
      let res = 0, n = nums.length
      let arr = Array(n + 1).fill(0)
      for (let i = 0; i < n; i++) {
        arr[i + 1] = arr[i] + nums[i]
      }
      for (let i = firstLen, t = 0; i + secondLen - 1 < n; i++) {
        t = Math.max(t, arr[i] - arr[i - firstLen])
        res = Math.max(res, t + arr[i + secondLen] - arr[i])
      }
      for (let i = secondLen, t = 0; i + firstLen - 1 < n; i++) {
        t = Math.max(t, arr[i] - arr[i - secondLen])
        res = Math.max(res, t + arr[i + firstLen] - arr[i])
      }
      return res
    }
    console.log(fn([0, 6, 5, 2, 2, 5, 1, 9, 4], 1, 2))
  </script>
</body>
</html>